Comparative Maps: Life Expectancy (1967 vs 2007)

# Base world polygons
world <- spData::world

# Join Gapminder life expectancy for two years
world_gap <- world %>%
  left_join(
    gapminder %>%
      filter(year %in% c(1967, 2007)) %>%
      select(country, year, lifeExp) %>%
      tidyr::pivot_wider(names_from = year, values_from = lifeExp),
    by = c("name_long" = "country")
  ) %>%
  mutate(delta = `2007` - `1967`)
# Define breaks for consistent comparison
vals <- c(world_gap$`1967`, world_gap$`2007`)
brks <- classInt::classIntervals(vals, n = 7, style = "quantile")$brks

m1 <- tm_shape(world_gap) +
  tm_polygons("1967", palette = "Blues", title = "Life Expectancy 1967", breaks = brks) +
  tm_layout(frame = FALSE)

m2 <- tm_shape(world_gap) +
  tm_polygons("2007", palette = "Blues", title = "Life Expectancy 2007", breaks = brks) +
  tm_layout(frame = FALSE)

tmap_arrange(m1, m2, ncol = 2)

tm_shape(world_gap) +
  tm_polygons("delta", palette = "RdBu", midpoint = 0,
              title = "Change in Life Expectancy (2007 - 1967)") +
  tm_layout(legend.outside = TRUE)

Interactive Life Expectancy graph

world_time <- world %>%
  left_join(
    gapminder %>% select(country, year, lifeExp),
    by = c("name_long" = "country")
  )


library(leaflet)

# Pick years you want to visualize
years_to_plot <- c(1967, 1987, 1997, 2007)

map <- leaflet() %>% addTiles()

# Loop over years and add polygons
for (yr in years_to_plot) {
  map <- map %>%
    addPolygons(
      data = subset(world_time, year == yr),
      fillColor = ~colorNumeric("YlOrRd", lifeExp.y)(lifeExp.y),
      fillOpacity = 0.7, color = "white", weight = 0.5,
      group = as.character(yr),
      popup = ~paste0("<b>", name_long, "</b><br>",
                      "Year: ", yr, "<br>",
                      "Life Expectancy: ", round(lifeExp.y, 1))
    )
}

map <- map %>%
  addLayersControl(
    overlayGroups = as.character(years_to_plot),
    options = layersControlOptions(collapsed = FALSE)
  )

map
world_gap <- world %>%
  left_join(
    gapminder %>%
      filter(year %in% c(1967, 2007)) %>%
      select(country, year, lifeExp) %>%
      tidyr::pivot_wider(names_from = year, values_from = lifeExp),
    by = c("name_long" = "country")
  ) %>%
  rename(lifeExp1967 = `1967`,
         lifeExp2007 = `2007`) %>%
  mutate(delta = lifeExp2007 - lifeExp1967)
world_time <- world %>%
  left_join(
    gapminder %>% select(country, year, lifeExp),
    by = c("name_long" = "country")
  )


# Define breaks
vals <- c(world_gap$lifeExp1967, world_gap$lifeExp2007)
brks <- classInt::classIntervals(vals, n = 7, style = "quantile")$brks

# Side-by-side
m1 <- tm_shape(world_gap) +
  tm_polygons("lifeExp1967", palette = "Blues", title = "Life Expectancy 1967", breaks = brks)

m2 <- tm_shape(world_gap) +
  tm_polygons("lifeExp2007", palette = "Blues", title = "Life Expectancy 2007", breaks = brks)

tmap_arrange(m1, m2, ncol = 2)

# Delta map
tm_shape(world_gap) +
  tm_polygons("delta", palette = "RdBu", midpoint = 0,
              title = "Change (2007 – 1967)") +
  tm_layout(legend.outside = TRUE)

# Join all Gapminder years to world


tmap_arrange(m1, m2, ncol = 2)

#map_facets
colnames(world_time)
##  [1] "iso_a2"    "name_long" "continent" "region_un" "subregion" "type"     
##  [7] "area_km2"  "pop"       "lifeExp.x" "gdpPercap" "geom"      "year"     
## [13] "lifeExp.y"
map_facets <- tm_shape(world_time) +
  tm_polygons("lifeExp.y", palette = "YlGnBu", title = "Life Expectancy") +
  tm_facets(by = "year", ncol = 2) +
  tm_layout(legend.outside = TRUE)

map_facets

library(tmap)

# Ensure output folder exists
if (!dir.exists("figlife")) dir.create("figlife")

# Loop through years and save maps as PNGs
for (yr in sort(unique(world_time$year))) {
  map <- tm_shape(subset(world_time, year == yr)) +
    tm_polygons("lifeExp.y", palette = "YlGnBu", title = "Life Expectancy") +
    tm_layout(
      legend.outside = TRUE,
      main.title = paste("Life Expectancy -", yr),
      main.title.size = 1.5
    )
  
  out_file <- file.path("figlife", paste0("lifeExp_", yr, ".png"))
  
  png(out_file, width = 1000, height = 700)
  print(map)   # render to PNG device
  dev.off()
  message("Saved: ", normalizePath(out_file))
}